package com.ease.component.controls.transitionclass {

import flash.display.DisplayObject;

import mx.core.UIComponent;
import mx.core.mx_internal;
import mx.states.IOverride;

use namespace mx_internal;

public class RemoveChildren implements IOverride {

   public function RemoveChildren(target:UIComponent = null) {
      super();

      this.target = target;
   }

   //---------------------------------------------------------------------------
   // Instance Variables

   [Inspectable (category = "General")]

   /** Reference to the container whose children to remove from this view. */
   public var target:UIComponent;

   /** List of index of the removed child. */
   private var _childIndices:Array /*Object*/;

   /** Property to check whether the children has been removed. */
   private var _childrenRemoved:Boolean;

   //---------------------------------------------------------------------------
   // Instance Methods

   /**
    * @inheritDoc
    */
   public function initialize():void {
      _childIndices = new Array();
   }

   /**
    * @inheritDoc
    */
   public function apply(parent:UIComponent):void {
      _childrenRemoved = false;

      if (target != null) {
         var index:int = 0;

         while (target.numChildren > 0) {
            var child:DisplayObject = target.removeChildAt(0);
           _childIndices.push({child:child, index: index++});
         }

         _childrenRemoved = true;
      }
   }

   /**
    * @inheritDoc
    */
   public function remove(parent:UIComponent):void {
      if (_childrenRemoved) {
         var len:int = _childIndices.length;

         for (var i:int = 0; i < len; i++) {
            var item:Object = _childIndices[i];
            target.addChildAt(item.child, item.index);

            if (item.child is UIComponent) {
               UIComponent(item.child).updateCallbacks();
            }
         }

         _childrenRemoved = false;
      }
   }
}
}